package com.huey.utils.qlexpress;

import com.alibaba.fastjson.JSONObject;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.apache.commons.lang3.StringUtils;

/**
 * @author xubo
 * @date 2022/8/23 15:45
 */
public class Test {

    public static void main(String[] args) throws Exception {
        /** 简单运算表达式 */
        //OperatorFactory 在ExpressRunner构造方法中 会初始化各种运算符号
        ExpressRunner runner_1 = new ExpressRunner();
        DefaultContext<String, Object> context_1 = new DefaultContext<String, Object>();
        context_1.put("商品数量", 10);
        context_1.put("商品价格", 2);
        context_1.put("优惠价格", 3);
        runner_1.addOperatorWithAlias("小于等于", "<=", null);
        runner_1.addOperatorWithAlias("大于等于", ">=", null);
        String express_1 = "商品数量 * 商品价格 大于等于 优惠价格";

        Object result_1 = runner_1.execute(express_1, context_1, null, true, false);
        System.out.println(result_1);

        //---------------------------------------------------------------------------------------------------------------------------
        /** 逻辑表达式 */
        ExpressRunner runner_2 = new ExpressRunner();

        runner_2.addOperatorWithAlias("如果", "if", null);
        runner_2.addOperatorWithAlias("则", "then", null);
        runner_2.addOperatorWithAlias("否则", "else", null);

        DefaultContext<String, Object> context_2 = new DefaultContext<String, Object>();

        context_2.put("语文", 100);
        context_2.put("数学", 100);
        context_2.put("英语", 60);
        String express_2 = "如果 (语文 + 数学 + 英语 > 270) 则 {return 1;} 否则 {return 0;}";
        Object result_2 = runner_2.execute(express_2, context_2, null, false, false, null);
        System.out.println(result_2);
        //---------------------------------------------------------------------------------------------------------------------------

        /** 复杂逻辑表达式 */
        ExpressRunner runner_3 = new ExpressRunner();
        runner_3 = new ExpressRunner();
        runner_3.addOperatorWithAlias("如果", "if", null);
        runner_3.addOperatorWithAlias("或", "||", null);
        runner_3.addOperatorWithAlias("且", "&&", null);
        runner_3.addOperatorWithAlias("等于", "==", null);
        runner_3.addOperatorWithAlias("大于", ">", null);
        runner_3.addOperatorWithAlias("小于", "<", null);
        runner_3.addOperatorWithAlias("大于等于", "<=", null);
        runner_3.addOperatorWithAlias("小于等于", ">=", null);
        runner_3.addOperatorWithAlias("则", "then", null);
        runner_3.addOperatorWithAlias("否则", "else", null);
        runner_3.addOperatorWithAlias("返回", "return", null);
        runner_3.addFunctionOfClassMethod("获取JSON中的值", Test.class.getName(), "getValue", new String[]{"String"}, null);
        runner_3.addFunctionOfClassMethod("字符串等于", Test.class.getName(), "equals", new String[]{"String", "String"}, null);

        DefaultContext<String, Object> context_3_1 = new DefaultContext<>();
        String express_3_1 = "如果 (获取JSON中的值(\"code\") 等于 1 ) 则 {返回 true} 否则 {返回 false}";
        Object result_3_1 = runner_3.execute(express_3_1, context_3_1, null, true, false);
        System.out.println(result_3_1);

        DefaultContext<String, Object> context_3_2 = new DefaultContext<>();
        String express_3_2 = "如果 ( 字符串等于( 获取JSON中的值(\"message\"), \"success\") ) 则 {返回 true} 否则 {返回 false}";
        Object result_3_2 = runner_3.execute(express_3_2, context_3_2, null, true, false);
        System.out.println(result_3_2);

    }

    public static Object getValue(String name) {
        String json = "{\"code\": 1,\"message\": \"success\"}";
        return JSONObject.parseObject(json).get(name);
    }

    public static boolean equals(String param1, String param2) {
        return StringUtils.equals(param1, param2);
    }

}
